package eu.connect.im;

import java.io.IOException;
import java.io.InputStream;
import java.io.PipedOutputStream;

public class XMPPMsgFilterThread extends Thread
{
	String name;
	InputStream sin;
	PipedOutputStream pipeout;
	
	public XMPPMsgFilterThread(String name, InputStream sin, PipedOutputStream pipeout)
	{
		this.name = name;
		this.sin = sin;
		this.pipeout = pipeout;
	}
	
	public void run()
	{
		try
		{
			byte b[] = new byte [1];
			while (!Thread.interrupted())
			{
				int length = this.sin.read(b);
				if (length < 0) break;
				char c = (char)b[0];
				if (c == '<')
				{
					char c1 = (char)this.sin.read();
					if (c1 == '?')
					{
						while ((c1 = (char)this.sin.read()) != '?');
						c1 = (char)this.sin.read();
					}
					else
					{
						this.pipeout.write(c);
						this.pipeout.write(c1);
					}
				}
				else
					this.pipeout.write(c);
			}
		}
		catch (Exception e)
		{
			//e.printStackTrace();
		}
		finally
		{
			try {
				this.pipeout.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(name+"'s MsgFilterThread has been stopped!");
		}
	}
	
}
